package com.shangcheng.users.controller;


import com.shangcheng.users.pojo.User;
import com.shangcheng.users.service.UserService;
import com.github.pagehelper.PageInfo;
import entity.BCrypt;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/****
 * @Author:admin
 * @Description:
 * @Date 2019/6/14 0:18
 *****/

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {


    @Autowired
    private UserService userService;

    /***
     * 用户登录
     */
    @PostMapping(value = "/login")
    public Result<User> login(@RequestParam String username, @RequestParam String password) {
        //查询用户信息
        User user = userService.findById(username);
        if (user != null && BCrypt.checkpw(password, user.getPassword())) {
            return new Result<User>(true, StatusCode.OK, "登录成功！", user);
        } else {
            return new Result<User>(false, StatusCode.LOGINERROR, "账号或者密码错误！");
        }
    }

    /***
     * User分页条件搜索实现
     * @param user
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo> findPage(@RequestBody(required = false) User user, @PathVariable int page, @PathVariable int size) {
        //调用UserService实现分页条件查询User
        PageInfo<User> pageInfo = userService.findPage(user, page, size);
        return new Result(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /***
     * User分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/findPage")
    public Result<PageInfo> findPage(@RequestParam int page, @RequestParam int size) {
        //调用UserService实现分页查询User
        PageInfo<User> pageInfo = userService.findPage(page, size);
        return new Result<PageInfo>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param user
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List<User>> findList(@RequestBody(required = false) User user) {
        //调用UserService实现条件查询User
        List<User> list = userService.findList(user);
        return new Result<List<User>>(true, StatusCode.OK, "查询成功", list);
    }

    /***
     * 根据ID删除品牌数据
     * @param username
     * @return
     */
    @DeleteMapping(value = "/dele")
    public Result delete(@RequestParam String username) {
        //调用UserService实现根据主键删除
        userService.delete(username);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /***
     * 修改User数据
     * @param user
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result update(@RequestBody User user, @PathVariable String id) {
        //设置主键值
        user.setUsername(id);
        //调用UserService实现修改User
        userService.update(user);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /***
     * 新增User数据
     * @param user
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody User user) {
        String hashedPassword = BCrypt.hashpw(user.getPassword(), BCrypt.gensalt());
        user.setPassword(hashedPassword);
        //调用UserService实现添加User
        userService.add(user);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    /***
     * 根据ID查询User数据
     * @param name
     * @return
     */
    @GetMapping("/findByName")
    public Result<User> findByName(@RequestParam String name) {
        //调用UserService实现根据主键查询User
        User user = userService.findByName(name);
        return new Result<User>(true, StatusCode.OK, "查询成功", user);
    }

    /***
     * 查询User全部数据
     * @return
     */
    @GetMapping
    public Result<List<User>> findAll() {
        //调用UserService实现查询所有User
        List<User> list = userService.findAll();
        return new Result<List<User>>(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 增加用户积分
     *
     * @param
     * @param points
     * @return
     */
    @GetMapping(value = "/addPoints")
    public Result addPoints(@RequestParam Integer points, @RequestParam String username) {
        userService.updatePoints(points, username);
        return new Result(true, StatusCode.OK, "增加积分成功");
    }
}
